package rploo_ciayoo;



/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Master_MataKuliah.java
 *
 * Created on Nov 19, 2012, 2:18:18 PM
 */

import Matakuliah.MakulKontrol;
import Matakuliah.Matakuliah;
import Matakuliah.MatakuliahTableModel;
import java.sql.SQLException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author Rico
 */
public class Master_MataKuliah extends javax.swing.JInternalFrame {

    /** Creates new form Master_MataKuliah */
    public Master_MataKuliah() {
        initComponents();
        lihatSemua();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        idMatakuliahTxt = new javax.swing.JTextField();
        namaMatakuliahTxt = new javax.swing.JTextField();
        simpanMatakuliahBtn = new javax.swing.JButton();
        updateMatakuliahBtn = new javax.swing.JButton();
        deleteMatakuliahBtn = new javax.swing.JButton();
        batalMatakuliahBtn = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        tabelMatakuliah = new javax.swing.JTable();
        keluarMatakuliahBtn = new javax.swing.JButton();

        jLabel3.setText("Id Matakuliah");

        jLabel4.setText("Nama Matakuliah");

        simpanMatakuliahBtn.setText("Simpan");
        simpanMatakuliahBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                simpanMatakuliahBtnActionPerformed(evt);
            }
        });

        updateMatakuliahBtn.setText("Update");
        updateMatakuliahBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                updateMatakuliahBtnActionPerformed(evt);
            }
        });

        deleteMatakuliahBtn.setText("Delete");
        deleteMatakuliahBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                deleteMatakuliahBtnActionPerformed(evt);
            }
        });

        batalMatakuliahBtn.setText("Batal");
        batalMatakuliahBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                batalMatakuliahBtnActionPerformed(evt);
            }
        });

        tabelMatakuliah.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null}
            },
            new String [] {
                "Id Matakuliah", "Nama Matakuliah"
            }
        ));
        tabelMatakuliah.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tabelMatakuliahMouseClicked(evt);
            }
        });
        jScrollPane2.setViewportView(tabelMatakuliah);

        keluarMatakuliahBtn.setText("Keluar");
        keluarMatakuliahBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                keluarMatakuliahBtnActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(19, 19, 19)
                .addComponent(jLabel3)
                .addGap(48, 48, 48)
                .addComponent(idMatakuliahTxt, javax.swing.GroupLayout.PREFERRED_SIZE, 166, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGroup(layout.createSequentialGroup()
                .addGap(20, 20, 20)
                .addComponent(jLabel4)
                .addGap(29, 29, 29)
                .addComponent(namaMatakuliahTxt, javax.swing.GroupLayout.PREFERRED_SIZE, 166, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGroup(layout.createSequentialGroup()
                .addGap(30, 30, 30)
                .addComponent(simpanMatakuliahBtn)
                .addGap(23, 23, 23)
                .addComponent(updateMatakuliahBtn)
                .addGap(23, 23, 23)
                .addComponent(deleteMatakuliahBtn)
                .addGap(27, 27, 27)
                .addComponent(batalMatakuliahBtn))
            .addGroup(layout.createSequentialGroup()
                .addGap(10, 10, 10)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 425, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGroup(layout.createSequentialGroup()
                .addGap(290, 290, 290)
                .addComponent(keluarMatakuliahBtn))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(11, 11, 11)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(3, 3, 3)
                        .addComponent(jLabel3))
                    .addComponent(idMatakuliahTxt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(19, 19, 19)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel4)
                    .addComponent(namaMatakuliahTxt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(20, 20, 20)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(simpanMatakuliahBtn)
                    .addComponent(updateMatakuliahBtn)
                    .addComponent(deleteMatakuliahBtn)
                    .addComponent(batalMatakuliahBtn))
                .addGap(17, 17, 17)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 206, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(4, 4, 4)
                .addComponent(keluarMatakuliahBtn))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void batalMatakuliahBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_batalMatakuliahBtnActionPerformed
        // TODO add your handling code here:
        idMatakuliahTxt.setText(null);
        namaMatakuliahTxt.setText(null);
}//GEN-LAST:event_batalMatakuliahBtnActionPerformed

    private void keluarMatakuliahBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_keluarMatakuliahBtnActionPerformed
        // TODO add your handling code here:
        this.dispose();
}//GEN-LAST:event_keluarMatakuliahBtnActionPerformed

    private void simpanMatakuliahBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_simpanMatakuliahBtnActionPerformed
        // TODO add your handling code here:
        Matakuliah MakulBaru = new Matakuliah();
        String id = idMatakuliahTxt.getText();
        String nama = namaMatakuliahTxt.getText();
        MakulBaru.setIdMatakuliah(id);
        MakulBaru.setNamaMatakuliah(nama);


        try {
            MakulKontrol kontrol;
            kontrol = MakulKontrol.getKoneksi();
            kontrol.tambahMakul(MakulBaru);

            JOptionPane.showMessageDialog(this, "Data Tersimpan");
            idMatakuliahTxt.setText("");
            namaMatakuliahTxt.setText("");

        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(this, "Data Gagal Disimpan!!! \n Mohon Ulangi");
            Logger.getLogger(Master_MataKuliah.class.getName()).log(Level.SEVERE, null, ex);
        }

        lihatSemua();
    }

    public void lihatSemua() {
        idMatakuliahTxt.requestFocus();
        try {
            List<Matakuliah> dos = MakulKontrol.getKoneksi().lihatDataMakul();
            MatakuliahTableModel table = new MatakuliahTableModel(dos);
            tabelMatakuliah.setModel(table);
        } catch (SQLException ex) {
            Logger.getLogger(Master_MataKuliah.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_simpanMatakuliahBtnActionPerformed

    private void updateMatakuliahBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_updateMatakuliahBtnActionPerformed
        // TODO add your handling code here:
        Matakuliah MakulBaru = new Matakuliah();
        String id = idMatakuliahTxt.getText();
        String nama = namaMatakuliahTxt.getText();
        MakulBaru.setIdMatakuliah(id);
        MakulBaru.setNamaMatakuliah(nama);


        try {
            MakulKontrol kontrol;
            kontrol = MakulKontrol.getKoneksi();
            kontrol.updateMakul(MakulBaru);

            JOptionPane.showMessageDialog(this, "Data Berhasil diubah");
            idMatakuliahTxt.setText("");
            namaMatakuliahTxt.setText("");

        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(this, "Data Gagal Diubah!!! \n Mohon Ulangi");
            Logger.getLogger(Master_MataKuliah.class.getName()).log(Level.SEVERE, null, ex);
        }

        lihatSemua();
    }//GEN-LAST:event_updateMatakuliahBtnActionPerformed

    private void deleteMatakuliahBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_deleteMatakuliahBtnActionPerformed
        // TODO add your handling code here:
        Matakuliah MakulBaru = new Matakuliah();
        String id = idMatakuliahTxt.getText();
        String nama = namaMatakuliahTxt.getText();
        MakulBaru.setIdMatakuliah(id);
        MakulBaru.setNamaMatakuliah(nama);


        try {
            MakulKontrol kontrol;
            kontrol = MakulKontrol.getKoneksi();
            kontrol.hapusPrepared(MakulBaru);

            JOptionPane.showMessageDialog(this, "Data berhasil dihapus");
            idMatakuliahTxt.setText("");
            namaMatakuliahTxt.setText("");

        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(this, "Data Gagal dihapus!!! \n Mohon Ulangi");
            Logger.getLogger(Master_MataKuliah.class.getName()).log(Level.SEVERE, null, ex);
        }

        lihatSemua();
    }//GEN-LAST:event_deleteMatakuliahBtnActionPerformed

    private void tabelMatakuliahMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tabelMatakuliahMouseClicked
        // TODO add your handling code here:
        int row = tabelMatakuliah.getSelectedRow();
        String kode = tabelMatakuliah.getValueAt(row, 0).toString();
        String nama = tabelMatakuliah.getValueAt(row, 1).toString();

        idMatakuliahTxt.setText(kode);
        namaMatakuliahTxt.setText(nama);
        simpanMatakuliahBtn.setEnabled(true);
//        simpanDosenBtn.setText("update");
    }//GEN-LAST:event_tabelMatakuliahMouseClicked
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton batalMatakuliahBtn;
    private javax.swing.JButton deleteMatakuliahBtn;
    private javax.swing.JTextField idMatakuliahTxt;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JButton keluarMatakuliahBtn;
    private javax.swing.JTextField namaMatakuliahTxt;
    private javax.swing.JButton simpanMatakuliahBtn;
    private javax.swing.JTable tabelMatakuliah;
    private javax.swing.JButton updateMatakuliahBtn;
    // End of variables declaration//GEN-END:variables
}
